<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\NetworkExpress;
use Illuminate\Support\Facades\Auth;
use App\Repositories\NetworkExpressRepository;
use App\Http\Requests\Admin\NetworkExpress\ShowRequest;
use App\Http\Requests\Admin\NetworkExpress\QueryResource;
use App\Http\Requests\Admin\NetworkExpress\NetworkExpressCreate;
use App\Http\Requests\Admin\NetworkExpress\NetworkExpressUpdate;
use App\Http\Requests\Admin\NetworkExpress\NetworkExpressDestroy;

class NetworkExpressController extends Controller
{
    protected $networkExpressRepository;

    /**
     * NetworkExpressExpressesController constructor.
     * @param NetworkExpressRepository $networkExpressRepository
     */
    public function __construct(
        NetworkExpressRepository $networkExpressRepository
    )
    {
        $this->networkExpressRepository = $networkExpressRepository;
    }

    /**
     * 获取关联网点列表资源列表
     *
     * Date: 2018/05/10
     * @author Tenny
     * @param QueryResource $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryResource $request)
    {
        $networkExpress_name = $request->get('networkExpress_name', false);
        $owner_name = $request->get('owner_name', false);
        $phone = $request->get('phone', false);
        $limit = $request->get('area_id', false);
        $networkExpressExpresses = $this->networkExpressRepository->getNetworkExpresses($networkExpress_name, $limit, $owner_name, $phone);
        return success($networkExpressExpresses);
    }

    /**
     * 获取关联网点详情
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param ShowRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(ShowRequest $request, NetworkExpress $networkExpress)
    {
        return success($networkExpress);
    }


    /**
     * 创建关联网点
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param NetworkExpressCreate $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(NetworkExpressCreate $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $networkExpressExpresses = $this->networkExpressRepository->create($attributes);
        return stored($networkExpressExpresses);
    }

    /**
     * 更新关联网点资源
     *
     * Date: 2018/05/11
     * @author Tenny
     * @param NetworkExpressUpdate $request
     * @param networkExpress $networkExpress
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(NetworkExpressUpdate $request, NetworkExpress $networkExpress)
    {
        $networkExpressExpressesAttr = $request->only([
            'service_id',
            'network_id',
            'province_id',
            'city_id',
            'district_id',
            'address',
            'status',
            'credit',
            'unit_price',
        ]);
        $networkExpress->update($networkExpressExpressesAttr);
        return updated($networkExpress);
    }

    /**
     * 删除 关联网点 资源
     *
     * Date: 2018/05/09
     * @author Tenny
     * @param NetworkExpressDestroy $request
     * @param NetworkExpress $networkExpress
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(NetworkExpressDestroy $request, NetworkExpress $networkExpress)
    {
        //软删除
        $status = $networkExpress->status;
        // 状态:  1正常   2已解除
        if (isset($status) || $status == 2) {
            return failed('删除异常');
        } else {
            $networkExpress->status = 2;
            $networkExpress->save();
        }
        return deleted();
    }
}
